<template>
  <div>
    <router-link to="admin/add">
      <el-button>添加</el-button>
    </router-link>

    <el-table :data="tableData" style="width: 100%" row-key="id" border>
      <el-table-column label="用户ID" prop="uid"></el-table-column>
      <el-table-column label="用户名" prop="username"></el-table-column>
      <el-table-column label="角色名称" prop="rolename"> </el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
          <el-tag v-else>禁用</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            circle
            @click="edit(scope.row.uid)"
            icon="el-icon-edit"
            size="mini"
          ></el-button>

          <el-button
            type="danger"
            @click="handleDelete(scope.row.uid)"
            icon="el-icon-delete"
            size="mini"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-size="pageSize"
      layout="prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<style>
</style>

<script>
import { getTotalAdminNumber, getUserList } from "../../request";
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      total: 0,
      pageSize: 2,
    };
  },
  created() {
    this.http.get("/userlist").then((res) => {

      // this.tableData = res.data.list;
    });
    this.getNumber();
    this.getData();
  },
  methods: {
    async getData() {
      let res = await getUserList({
        size: this.pageSize,
        page: this.currentPage,
      });
      this.tableData = res.data.list;
    },

    async getNumber() {
      let res = await getTotalAdminNumber();
      this.total = res.data.list[0].total;
    },

    // 编辑
    edit(uid) {
      this.$router.push("/layout/admin/" + uid);
    },
    // 删除，需要二次确认
    handleDelete(id) {
      this.$confirm("确认关闭？")
        .then((_) => {
          //  确认删除
          this.doDel(id);
        })
        .catch((_) => {
          //  关闭弹窗
        });
    },
    doDel(id) {
      this.http.post("/roledelete", { id: id }).then((res) => {
        this.tableData = res.list;
      });
    },
    handleCurrentChange(val) {
      this.getData();
    },
  },
};
</script>